package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.dto.CaseCommentDto;
import cn.livingCloud.dto.CaseFrontInfoDto;
import cn.livingCloud.dto.CaseImageDto;
import cn.livingCloud.entity.core.CaseComments;
import cn.livingCloud.entity.core.CaseImage;
import cn.livingCloud.entity.core.CaseRecord;
import cn.livingCloud.repository.CaseCommentsMapper;
import cn.livingCloud.repository.CaseImageMapper;
import cn.livingCloud.repository.CaseRecordMapper;

import java.util.List;

/**
 * Created by 唐旭 on 2016/6/14.
 */
public class CaseFrontInfoDtoLoader extends CaseBaseDtoLoader<CaseFrontInfoDto> {
    private transient CaseImageMapper imageMapper = BeanProvider.getBean(CaseImageMapper.class);
    private transient CaseCommentsMapper commentsMapper = BeanProvider.getBean(CaseCommentsMapper.class);
    private transient CaseRecordMapper recordMapper = BeanProvider.getBean(CaseRecordMapper.class);

    public CaseFrontInfoDtoLoader(String uuid) {
        super(uuid);
    }

    public CaseFrontInfoDto load() {
        CaseFrontInfoDto load = super.load(CaseFrontInfoDto.class);
        loadImageList(load);
        loadCommentList(load);
        loadCompleteImageList(load);
        loadCancelContent(load);
        return load;
    }

    private void loadCancelContent(CaseFrontInfoDto load) {
        if (load.getStatus().isCancelled()) {
            CaseRecord record = recordMapper.findCaseCancelRecord(uuid);
            load.setCancelContent(record.getContent());
        }
    }

    private void loadCompleteImageList(CaseFrontInfoDto load) {
        if (load.getStatus().isFinish()) {
            List<CaseImageDto> completeImageList = load.getCompleteImageList();
            List<CaseImage> images = imageMapper.findImagesByCaseUuid(uuid, true);
            for (CaseImage image : images) {
                completeImageList.add(new CaseImageDto(image));
            }
        }
    }

    private void loadCommentList(CaseFrontInfoDto load) {
        List<CaseCommentDto> commentList = load.getCommentList();
        List<CaseComments> list = commentsMapper.findListByCaseUuid(uuid);
        for (CaseComments comments : list) {
            commentList.add(new CaseCommentDto(comments));
        }
    }

    private void loadImageList(CaseFrontInfoDto load) {
        List<CaseImageDto> imageList = load.getImageList();
        List<CaseImage> images = imageMapper.findImagesByCaseUuid(uuid, false);
        for (CaseImage image : images) {
            imageList.add(new CaseImageDto(image));
        }
    }
}
